package com.mars.controller;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.mars.common.JsonData;
import com.mars.exception.ParamException;
import com.mars.param.RoleParam;
import com.mars.pojo.SysRole;
import com.mars.pojo.SysUser;
import com.mars.service.*;
import com.mars.util.BeanValidator;
import com.mars.util.StringUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 角色管理
 * 目前角色无层级结构，则需保证属于同一个层级
 * @author MARS
 * @date 2018/2/2
 */

@Controller
@RequestMapping("/sys/role")
public class SysRoleController {

    @Resource
    private SysRoleService sysRoleService;
    @Resource
    private SysTreeService sysTreeService;
    @Resource
    private SysRoleAclService sysRoleAclService;
    @Resource
    private SysRoleUserService sysRoleUserService;
    @Resource
    private SysUserService sysUserService;

    @RequestMapping("/role.page")
    public ModelAndView page() {
        return new ModelAndView("role");
    }

    @RequestMapping("/save.json")
    @ResponseBody
    public JsonData save(RoleParam param) {
        sysRoleService.save(param);
        return JsonData.success();
    }

    @RequestMapping("/update.json")
    @ResponseBody
    public JsonData update(RoleParam param) {
        sysRoleService.update(param);
        return JsonData.success();
    }

    @RequestMapping("/list.json")
    @ResponseBody
    public JsonData list() {
        return JsonData.success(sysRoleService.getAll());
    }


    // 获取当前角色权限树
    @RequestMapping("/roleTree.json")
    @ResponseBody
    public JsonData roleTree(@RequestParam("roleId") int roleId) {
        return JsonData.success(sysTreeService.roleTree(roleId));
    }

    // 保存角色的权限
    @RequestMapping("/changeAcls.json")
    @ResponseBody
    public JsonData changeAcls(@RequestParam("roleId") int roleId,@RequestParam(value = "aclIds",required = false,defaultValue = "") String aclIds) {
        List<Integer> aclIdList = StringUtil.splitToListInt(aclIds);
        sysRoleAclService.changeRoleAcls(roleId,aclIdList);
        return JsonData.success();
    }

    // 获取用户相关数据
    @RequestMapping("/users.json")
    @ResponseBody
    public JsonData users(@RequestParam("roleId") int roleId) {
        // 获取角色已经选择的用户列表（即角色已经分配的用户列表）
        List<SysUser> selectedUserList = sysRoleUserService.getListByRoleId(roleId);
        // 获取系统的所有用户
        List<SysUser> allUserList = sysUserService.getAll();
        // 获取未选择的用户
        List<SysUser> unselectedUserList = Lists.newArrayList();

        // jdk8,加速 list-->set
        Set<Integer> selectedUserIdSet = selectedUserList.stream().map(sysUser1 -> sysUser1.getId()).collect(Collectors.toSet());
        for (SysUser sysUser : allUserList) {
            // 用户状态为1 且选中的用户中不包含该用户(用户状态 1 为正常状态)
            if (sysUser.getStatus() == 1 && !selectedUserIdSet.contains(sysUser.getId())){
                unselectedUserList.add(sysUser);
            }
        }
        // 过滤用户状态不等于1的数据，留下status=1的数据
        //selectedUserList = selectedUserList.stream().filter(sysUser -> sysUser.getStatus() != 1).collect(Collectors.toList());
        Map<String,List<SysUser>> map = Maps.newHashMap();
        map.put("selected",selectedUserList);
        map.put("unselected",unselectedUserList);
        return JsonData.success(map);
    }

    // 保存角色的用户
    @RequestMapping("/changeUsers.json")
    @ResponseBody
    public JsonData changeUsers(@RequestParam("roleId") int roleId,@RequestParam(value = "userIds",required = false,defaultValue = "") String userIds) {
        List<Integer> userIdList = StringUtil.splitToListInt(userIds);
        sysRoleUserService.changeRoleUsers(roleId,userIdList);
        return JsonData.success();
    }
}
